package com.atguigu.category.service.impl;

import com.atguigu.category.mapper.CategoryMapper;
import com.atguigu.category.service.CategoryService;
import com.atguigu.clients.CategoryClient;
import com.atguigu.clients.ProductClient;
import com.atguigu.param.PageParam;
import com.atguigu.param.ProductHotParam;
import com.atguigu.pojo.Category;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * ClassName:CategoryImpl
 * Package:com.atguigu.category.service.impl
 * Description:
 *
 * @Author 应永豪
 * @Create 2024/4/21 20:54
 * @Version 1.0
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductClient productClient;

    @Override
    public R byname(String categoryName) {
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("category_name",categoryName);
        Category category = categoryMapper.selectOne(categoryQueryWrapper);
        if (category==null){
            log.info("CategoryServiceImpl.byname业务执行完毕，结果：{}","类别查询失败");
            return R.fail("类型查询失败");
        }
        log.info("CategoryServiceImpl.byname业务执行完毕，结果：{}","类别查询成功");
        return R.ok("查询成功",category);

    }

    @Override
    public R hotsCategory(ProductHotParam param) {
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.in("category_name",param.getCategoryName()).select("category_id");
        List<Object> ids = categoryMapper.selectObjs(categoryQueryWrapper);
        R ok = R.ok("类别集合查询成功", ids);
        log.info("CategoryServiceImpl.byname业务执行完毕，结果：{}",ok);
        return ok;

    }

    @Override
    public R list() {
        List<Category> categories = categoryMapper.selectList(null);
        R r = R.ok("类别全部数据查询成功", categories);
        log.info("CategoryServiceImpl.list业务执行完毕，结果：{}",r);
        return r;

    }

    @Override
    public R listPage(PageParam param) {
        IPage<Category> page = new Page<>(param.getCurrentPage(),param.getPageSize());

        page = categoryMapper.selectPage(page,null);

        return R.ok("类别分页查询成功",page.getRecords(),page.getTotal());
    }

    @Override
    public R adminSave(Category category) {
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("category_name", category.getCategoryName());
        Long count = categoryMapper.selectCount(categoryQueryWrapper);
        if (count>0){
            return R.fail("类别已经存在，添加失败");
        }

        int insert = categoryMapper.insert(category);

        log.info("CategoryServiceImpl.adminSave业务执行完毕，结果：{}",insert);
        return R.ok("类别添加成功");

    }

    @Override
    public R adminRemove(Integer categoryId) {
        Long count = productClient.adminCount(categoryId);
        if (count>0){
            return R.fail("类别删除失败,有"+count+"件商品正在引用");
        }

        int i = categoryMapper.deleteById(categoryId);
        log.info("CategoryServiceImpl.adminRemove业务执行完毕，结果：{}",i);
        return R.ok("类别数据删除成功");
    }

    @Override
    public R adminUpdate(Category category) {
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("category_name", category.getCategoryName());
        Long count = categoryMapper.selectCount(categoryQueryWrapper);
        if (count>0){
            return R.fail("类别已经存在，修改失败");
        }

        int i = categoryMapper.updateById(category);

        log.info("CategoryServiceImpl.adminUpdate业务执行完毕，结果：{}",i);
        return R.ok("类别修改成功");
    }
}
